package com.headwind.service;

import cn.hutool.core.lang.Dict;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.headwind.entity.HwUserBlog;
import com.headwind.entity.HwUserBlogKey;
import com.headwind.repository.HwUserBlogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author : Headwind
 * @version : 1.0
 * @date : Created in 2020/8/15
 * @description : 用户博客实现类
 */
@Service
public class HwUserBlogService {
    private final HwUserBlogRepository userBlogRepository;
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    @Autowired
    public HwUserBlogService(HwUserBlogRepository userBlogRepository) {
        this.userBlogRepository = userBlogRepository;
    }

    /**
     * 查询用户博客
     *
     * @return 博客内容
     */
    public Dict findOne(Integer id, String title, String createUserId) {
        Optional<HwUserBlog> result = userBlogRepository.findById(new HwUserBlogKey(id, title, createUserId));
        if (result.isPresent()) {
            return Dict.create().set("code", 200).set("data", gson.toJson(result.get()));
        }
        return Dict.create().set("code", 500).set("data", gson.toJson(new HwUserBlog()));
    }
}
